<template>
  <div class="wh100">
    <el-form label-position="top" ref="form" :model="form" label-width="80px">
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item
              label="预警类型："
              prop="warnType"
          >
            <el-select v-model="form.typeId" multiple>
              <el-option v-for="item in selectList" :label="item.typeName" :value="item.id" :key="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>


        <el-col :span="24" style="text-align: right">
          <el-form-item>
            <div class="btn">
              <div class="cancel">
                <el-button @click="cancelModel()">取消</el-button>
              </div>
              <div class="preservation">
                <el-button
                type="primary"
                :loading="loading"
                @click="onSubmit('form')"
            >保存
            </el-button
            >
              </div>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import {bindWarnType} from "@/api/camera";
import {getWarningTypeList} from "@/api/warning";

export default {
  props: {

    bindInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  mounted() {
    this.getWarningTypeList();
  },
  data() {
    return {
      warnList: [
        {
          id: 0,
          name: "不预警",
        },
        {
          id: 1,
          name: "出现则预警",
        },
        {
          id: 2,
          name: "月末预警",
        },
      ],
      value1: 1,
      selectList: [],
      form: {
        cameraId:this.bindInfo.id,
        typeId:0
      },
      loading: false,
    };
  },

  methods: {
    async getWarningTypeList() {
      let res = await getWarningTypeList({
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      });

      if (res.code == 200) {
        this.selectList = res.data.lists;
      }
    },
    async bindWarnType() {
      let res = await bindWarnType({
        warnTypeIds: this.form.typeId,
        cameraId: this.bindInfo.id,
      });

      if (res.code == 200) {
        this.$message({
          message: "添加成功",
          type: "success",
        });

        this.loading = false;
        this.$emit("onClose", {
          isClose: true,
        });
      }
      this.form = {}
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.bindWarnType()
        }
      });
    },
    cancelModel() {
      this.$emit("onClose");
      this.form = {}
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.left10 {
  margin-left: 10px;
}

.btn {
  display: flex;
  gap: 20px;
  justify-content: center;
  position: relative;
  top: 360px;

  .cancel {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 0);
      color: rgba(92, 251, 255, 1);
    }
  }

  .preservation {
    ::v-deep .el-button {
      width: 146px;
      border: 1px solid rgba(92, 251, 255, 1);
      background-color: rgba(92, 251, 255, 1);
      color: rgba(23, 24, 26, 1);
    }
  }
}

::v-deep .el-select {
  width: 100%;
}
::v-deep .is-disabled{
  opacity: 0.4;
  background-color: rgba(23, 24, 26, 0.1);
}
</style>
